﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Delay;

namespace lfm.Parser
{
    internal class Tag
    {
        public static async Task<lfm.Tag> getInfo(String e)
        {
            lfm.Tag result = new lfm.Tag();

            // Make Method Run Asynchronously
            await Task.Run(() =>
            {
                XDocument x = XDocument.Parse(e);

                result = (from u in x.Descendants("tag")
                            select new lfm.Tag
                            {
                                Name = u.SafeGetElement("name").SafeGetStringValue(),
                                URL = u.SafeGetElement("url").SafeGetUriValue(),
                                Reach = u.SafeGetElement("reach").SafeGetInt64Value(),
                                Taggings = u.SafeGetElement("taggings").SafeGetInt64Value(),
                                Streamable = Converters.ToBool(u.SafeGetElement("streamable").SafeGetStringValue()),
                                WikiPublished = u.SafeGetElement("wiki").SafeGetElement("published").SafeGetDateTimeValue(DateTime.Now),
                                WikiSummary = u.SafeGetElement("wiki").SafeGetElement("summary").SafeGetStringValue(),
                                WikiContent = u.SafeGetElement("wiki").SafeGetElement("content").SafeGetStringValue()
                            }).FirstOrDefault();

            });

            return result;
        }

        public static async Task<List<lfm.Artist>> getTopArtists(String e)
        {
            List<lfm.Artist> result = new List<lfm.Artist>();

            // Make Method Run Asynchronously
            await Task.Run(() =>
            {
                XDocument x = XDocument.Parse(e);

                // Parse main artist details
                var results = from u in x.Descendants("artist")
                              select new lfm.Artist
                              {
                                  Name = u.SafeGetElement("name").SafeGetStringValue(),
                                  Url = u.SafeGetElement("url").SafeGetUriValue(),
                                  Streamable = Converters.ToBool(u.SafeGetElement("streamable").SafeGetStringValue()),
                                  ImageSmall = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "small").FirstOrDefault().SafeGetUriValue(),
                                  ImageMedium = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "medium").FirstOrDefault().SafeGetUriValue(),
                                  ImageLarge = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "large").FirstOrDefault().SafeGetUriValue(),
                                  ImageExtraLarge = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "extralarge").FirstOrDefault().SafeGetUriValue(),
                                  ImageMega = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "mega").FirstOrDefault().SafeGetUriValue(),
                              };

                foreach (lfm.Artist r in results) result.Add(r);
            });

            // return the full artists
            return result;
        }

        public static async Task<List<lfm.Artist>> getWeeklyArtistChart(String e)
        {
            List<lfm.Artist> result = new List<lfm.Artist>();

            // Make Method Run Asynchronously
            await Task.Run(() =>
            {
                XDocument x = XDocument.Parse(e);

                // Parse main artist details
                var results = from u in x.Descendants("artist")
                              select new lfm.Artist
                              {
                                  Name = u.SafeGetElement("name").SafeGetStringValue(),
                                  PercentageChange = u.SafeGetElement("weight").SafeGetStringValue(),
                                  Url = u.SafeGetElement("url").SafeGetUriValue(),
                                  Streamable = Converters.ToBool(u.SafeGetElement("streamable").SafeGetStringValue()),
                                  ImageSmall = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "small").FirstOrDefault().SafeGetUriValue(),
                                  ImageMedium = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "medium").FirstOrDefault().SafeGetUriValue(),
                                  ImageLarge = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "large").FirstOrDefault().SafeGetUriValue(),
                                  ImageExtraLarge = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "extralarge").FirstOrDefault().SafeGetUriValue(),
                                  ImageMega = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "mega").FirstOrDefault().SafeGetUriValue(),
                              };

                foreach (lfm.Artist r in results) result.Add(r);
            });

            // return the full artists
            return result;
        }
    }
}
